﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace WotModSync
{

    public delegate void ProcessDirectoryDelegate(DirectoryInfo directory);
    public delegate void ProcessFileDelegate(FileInfo[] files);

    public class WotFiles
    {
        //private static string WotVersion(string wotDirectoy)
        //{
        //    var xdoc = XDocument.Load(Path.Combine(Properties.Settings.Default.WotGameDirectory22, "version.xml"));
        //    var node = xdoc.Descendants("version").Single();

        //    return node.Value;
        //}

        //private static string WotVersionDirectory()
        //{

        //    string version = WotVersion();

        //    if (version.IndexOf("#") > 0)
        //    {
        //        version = version.Substring(0, version.IndexOf("#"));
        //    }

        //    version = version.Replace("v.", "");

        //    return version.Trim();
        //}


        private static void WalkDirectoryTree(DirectoryInfo root, ProcessDirectoryDelegate dir, ProcessFileDelegate file)
        {
            System.IO.FileInfo[] files = null;
            System.IO.DirectoryInfo[] subDirs = null;



            // First, process all the files directly under this folder 
            try
            {
                files = root.GetFiles("*.*");
            }
            // This is thrown if even one of the files requires permissions greater 
            // than the application provides. 
            catch (UnauthorizedAccessException e)
            {
                // This code just writes out the message and continues to recurse. 
                // You may decide to do something different here. For example, you 
                // can try to elevate your privileges and access the file again.
                Console.WriteLine(e.Message);
            }
            catch (System.IO.DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }

            if (files != null)
            {

                dir(root);

                file(files);

                // Now find all the subdirectories under this directory.
                subDirs = root.GetDirectories();

                foreach (System.IO.DirectoryInfo dirInfo in subDirs)
                {
                    // Resursive call for each subdirectory.
                    WalkDirectoryTree(dirInfo, dir, file);
                }
            }
        }

        public static List<FileInfo> GetWotFiles(string wotDirectory)
        {
            var list = new List<FileInfo>();
            var root = new DirectoryInfo(Path.Combine(wotDirectory, "res_mods"));

            if (!root.Exists)
            {
                return list;
            }


            WalkDirectoryTree(root, dir => { }, files =>
            {
                list.AddRange(files);
            });

            return list;
        }

        public static void StartWOT(string wotDirectory)
        {
            string filename = Path.Combine(wotDirectory, "WoTLauncher.exe");

            if (!File.Exists(filename)) throw new FileNotFoundException(filename);

            Process.Start(filename);
        }

    }
}
